<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author lzw
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'operating:ftdsHjdssyydj:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsOperatingFtdsHjdssyydjForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { FtdsHjdssyydj, ftdsHjdssyydjSave, ftdsHjdssyydjForm } from '/@/api/operating/ftdsHjdssyydj';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('operating.ftdsHjdssyydj');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<FtdsHjdssyydj>({} as FtdsHjdssyydj);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增户籍地诉讼运营登记') : t('编辑户籍地诉讼运营登记'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('资方'),
      field: 'hjdssyydjZf',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('诉讼主体 '),
      field: 'hjdssyydjSsztyg',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('订单号'),
      field: 'hjdssyydjDdh',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('客户姓名'),
      field: 'hjdssyydjKhxm',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('身份证号'),
      field: 'hjdssyydjSfzh',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('手机号'),
      field: 'hjdssyydjSjh',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('省份'),
      field: 'hjdssyydjSf',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('身份证地址'),
      field: 'hjdssyydjSfzdz',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('剩余本金'),
      field: 'hjdssyydjSybj',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('诉请金额'),
      field: 'hjdssyydjSqje',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('管辖法院'),
      field: 'hjdssyydjGxfy',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('提交日期'),
      field: 'hjdssyydjTjrq',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('微法院状态'),
      field: 'hjdssyydjWfyzt',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('法院审批意见'),
      field: 'hjdssyydjFyspyj',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('法院审核 意见归类'),
      field: 'hjdssyydjFyshyjgl',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('法院案件沟通情况'),
      field: 'hjdssyydjFyajgtqk',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('立案方式'),
      field: 'hjdssyydjLafs',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('材料是否邮寄'),
      field: 'hjdssyydjClsfyj',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('单号'),
      field: 'hjdssyydjDh',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('邮寄时间'),
      field: 'hjdssyydjYjsj',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('法院电话'),
      field: 'hjdssyydjFydh',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('案件当前状态'),
      field: 'hjdssyydjAjdqzt',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('法院跟进记录'),
      field: 'hjdssyydjFygjjl',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('被告手机号状态'),
      field: 'hjdssyydjBgsjhzt',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('还款意向'),
      field: 'hjdssyydjHkyx',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('被告跟进记录'),
      field: 'hjdssyydjBggjjl',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('案件负责人'),
      field: 'hjdssyydjAjfzr',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('图片上传'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'ftdsHjdssyydj_image',
        uploadType: 'image',
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('附件上传'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'ftdsHjdssyydj_file',
        uploadType: 'all',
      },
      colProps: { lg: 24, md: 24 },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await ftdsHjdssyydjForm(data);
    record.value = (res.ftdsHjdssyydj || {}) as FtdsHjdssyydj;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await ftdsHjdssyydjSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
